package com.io.example.demoparent.exception.handler;

import com.io.example.demoparent.exception.MyException;
import com.io.example.demoparent.vo.ResultData;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class ValidExceptionHandler {
    @ExceptionHandler(BindException.class)
    public ResultData validExceptionHandler(BindException exception) {
        return ResultData.fail(900020, exception.getBindingResult().getFieldError().getDefaultMessage());
    }

    @ExceptionHandler(MyException.class)
    @ResponseBody
    public ResultData error(MyException e) {
        e.printStackTrace();
        return ResultData.fail(e.getCode(), e.getMessage());
    }
}
